﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebDemo
{
    public partial class TreeViewStudy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            TreeNode treeNode = new TreeNode("TreeNode",Guid.NewGuid().ToString());
            //treeNode.PopulateOnDemand = true;
            e.Node.ChildNodes.Add(treeNode);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            TreeNodeCollection tnc= this.TreeView1.Nodes;
            //DisplaySelectedNodes(tnc);
            this.Literal1.Text= this.TreeView1.CheckedNodes.Count.ToString();
        }

        private void DisplaySelectedNodes(TreeNodeCollection tnc)
        {
            foreach (TreeNode currentNode in tnc)
            {
                if (currentNode.Checked == true)
                {
                    this.Literal1.Text += currentNode.Value;
                }

                if (currentNode.ChildNodes.Count > 0)
                {
                    DisplaySelectedNodes(currentNode.ChildNodes);
                }
            }
        }
    }
}